package com.haier.config;

import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.haier.config.prop.ClassRoomProperties;
import com.haier.pojo.ClassRoom;
import com.haier.utils.PojoUtil;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Properties;

@Configuration
@MapperScan(basePackages = {"com.haier.dao.classRoom"}, sqlSessionTemplateRef = "classRoomSqlSessionTemplate")
public class ClassRoomMybatisConfig {

    @Resource
    private ClassRoomProperties classRoomProperties;

    @Bean
    @ConfigurationProperties(prefix = "datasource.classroom")
    public DataSource classRoomDataSource() {
        AtomikosDataSourceBean ds = new AtomikosDataSourceBean();
        ds.setXaProperties(PojoUtil.obj2Properties(classRoomProperties));
        ds.setXaDataSourceClassName("com.alibaba.druid.pool.xa.DruidXADataSource");
        ds.setUniqueResourceName("classRoomProperties");
        ds.setPoolSize(5);
//        return DataSourceBuilder.create().build();
        return ds;
    }
    @Bean
    public SqlSessionFactory classRoomSqlSessionFactory(@Qualifier("classRoomDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);

        //添加XML目录
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            bean.setMapperLocations(resolver.getResources("classpath:mapper/classRoom/*.xml"));
            return bean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /***
     * 单一事务配置
     * @param sqlSessionFactory
     * @return
     * @throws Exception
     */
//    @Bean("classRoomDataSourceTransactionManager")
//    public PlatformTransactionManager dataSourceTransactionManager(@Qualifier("classRoomDataSource") DataSource dataSource){
//        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
//        dataSourceTransactionManager.setDataSource(dataSource);
//        return dataSourceTransactionManager;
//    }
    @Bean
    public SqlSessionTemplate classRoomSqlSessionTemplate(@Qualifier("classRoomSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory);
        return template;
    }
}